// 极简 localStorage 封装（SSR 安全降级）

class LocalStorage {
  constructor() {
    const canUseBrowserStorage = typeof window !== 'undefined' && window.localStorage;
    this.storage = canUseBrowserStorage ? window.localStorage : LocalStorage.memoryStorage;
  }

  getItem(key) {
    try {
      return this.storage.getItem(key);
    } catch (e) {
      return null;
    }
  }

  setItem(key, value) {
    try {
      this.storage.setItem(key, String(value));
      return true;
    } catch (e) {
      return false;
    }
  }

  removeItem(key) {
    try {
      this.storage.removeItem(key);
    } catch (e) {
      // no-op
    }
  }

  clear() {
    try {
      this.storage.clear();
    } catch (e) {
      // no-op
    }
  }
}

LocalStorage.memoryStorage = (() => {
  const store = new Map();
  return {
    getItem: (k) => (store.has(k) ? store.get(k) : null),
    setItem: (k, v) => { store.set(k, String(v)); },
    removeItem: (k) => { store.delete(k); },
    clear: () => { store.clear(); },
  };
})();

export default LocalStorage;
